package com.qf.cloud2204.coupons.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    //如果想使用IOC容器中的LettuceConnectionFactory对象，可以在参数中声明即可
    @Bean
    public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory connectionFactory){

        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 注意：需要先把 RedisConnectionFactory 对象设置给 template
        template.setConnectionFactory(connectionFactory);

        //设置key的序列化方式
        template.setKeySerializer(new StringRedisSerializer());
        //设置string类型的value的序列化方式
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //设置hash结构的内部key和value序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        return template;
    }

}
